import sys
from PySide6.QtGui import QKeyEvent,  QPixmap, QPainter
from PySide6.QtWidgets import (QTextBrowser, QVBoxLayout, QApplication, QTabWidget)
from listwidget import Tab1, Qt, QWidget 

class Tab2(QWidget):
    def  __init__(self, parent=None):
        super().__init__(parent)
        self.pix = QPixmap()
    
    def paintEvent(self, event):
        if self.pix.isNull():
            return
        painter = QPainter(self)
        # painter.begin(self.tab_2)
        sz = self.size()
        pix = self.pix.scaled(sz.width(), sz.height(), Qt.KeepAspectRatio,
                        Qt.SmoothTransformation)
        # painter.setRenderHint(QPainter.Antialiasing)
        painter.drawPixmap(0, 0, pix)
        painter.end()
        
    def resizeEvent(self, event):
        self.update()
        
class Window(QTabWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(640, 320)
        self.setMinimumSize(280, 120)
        self.setWindowTitle("OCR")
        self.setTabPosition(QTabWidget.East)
        tabwid = self.tabBar().height()
        self.tab1 = Tab1()
        self.tab_2 = Tab2()
        self.tab_3 = QWidget()
        self.tab_2.resize(self.size().width(), self.size().height())

        self.addTab(self.tab1, 'pictures')
        self.addTab(self.tab_2, 'preview')
        self.addTab(self.tab_3, 'result')

        self.size1 = self.tab_2.size().width() - tabwid
        self.size2 = self.tab_2.size().height()
        self.vlayout = QVBoxLayout()
        self.ratio = self.size1 / self.size2
        
        self.textbro = QTextBrowser(self.tab_3)
        self.vlayout.addWidget(self.textbro)
        self.tab_3.setLayout(self.vlayout)
        self.tab1.tab_1.getalltext.connect(self.tab3show) 
        self.activateWindow()
        self.tab1.tab_1.currentab2.connect(self.setab2)
        
    def tab3show(self):
        alltext = ''
        self.textbro.clear()
        for i in self.tab1.tab_1.alltext:
            alltext += i + '\n'
        self.textbro.setText(alltext)
        self.setCurrentWidget(self.tab_3)
        
    def setab2(self, path):
        self.tab_2.pix = QPixmap(path)
        self.tab_2.update()
        self.setCurrentWidget(self.tab_2)
        
                
    def cpath(self, index):
        path = self.tab1.tab_1.path[index]
        self.tab_2.pix = QPixmap(path)
        self.update()
        self.tab1.tab_1.setCurrentItem(self.tab1.tab_1.item(index))

    def lastpic(self, len_):
        print('last picuture')
        if self.tab1.tab_1.currentRow() == 0:
            index = len_- 1
            self.cpath(index)
        else:
            index = self.tab1.tab_1.currentRow() - 1
            self.cpath(index)
    
    def nextpic(self, len_):
        print('next picture')
        if self.tab1.tab_1.currentRow() == len_ - 1:
            index = 0
            self.cpath(index)
        else:
            index = self.tab1.tab_1.currentRow() + 1
            self.cpath(index)
            
    def currentab2(self, event):
        count_ = self.tab1.tab_1.count()
        if count_ > 1:
            len_ = len(self.tab1.tab_1.path)
            if event.key() == Qt.Key_Left:
                self.lastpic(len_)
            elif event.key() == Qt.Key_Right:
                self.nextpic(len_)
        else:
            print("only one picture")
    
            
    def keyPressEvent(self, event:QKeyEvent):
        if self.currentWidget() == self.tab_2:
            self.currentab2(event)
            
if __name__ == '__main__':
    app = QApplication([])
    w = Window()
    w.show()
    sys.exit(app.exec())
    
    
    